package net.xdclass.base_project.producer;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sqz
 * @Description:
 * @date 2019/9/23 15:25
 */
@Component
@RestController
public class ProducerController {

    /**
     * 注入kafkaTemplate
     */
    @Autowired
    private KafkaSender kafkaSender;

    @GetMapping("/sendMessage")
    public  String sendMessageToKafka() {
        for (int i = 0; i < 50; i++) {
            String taskid = String.valueOf(i);
            Map<String,String> messageMap=new HashMap();
            messageMap.put("message",String.valueOf(i));
            messageMap.put("发送时间",String.valueOf(System.currentTimeMillis()));
            String jsonStr=JSONObject.toJSONString(messageMap);
            kafkaSender.send("first",taskid,jsonStr);
        }
        //kakfa的推送消息方法有多种，可以采取带有任务key的，也可以采取不带有的（不带时默认为null）
        return "test successfully!";
    }
}

